package course_9_2.whatIsIt;

import java.util.InputMismatchException;
import java.util.Scanner;
 class KeyboardInput {
	static Scanner scanner = new Scanner(System.in);
	public int readInt(String prompt) {
		int value ;
		while (true){
			try {
				System.out.print(prompt);
				value = scanner.nextInt();
				break;
			} catch (InputMismatchException e) {
				System.out.println("输入错误，请重新输入整数。");
			}
			finally {
				scanner.nextLine();
			}
		}
		return value;
	}
	public double readDouble(String prompt) {
		double value = 0;
		while (true){
			try {
				System.out.print(prompt);
				value = scanner.nextDouble();
				break;
			} catch (InputMismatchException e) {
				System.out.println("输入错误，请重新输入小数。");
				scanner.nextLine();
			}
		}
		return value;
	}
	public String readString(String prompt) {
		String value;
		while (true){
			try {
				System.out.print(prompt);
				value = scanner.nextLine();
				break;
			} catch (InputMismatchException e) {
				System.out.println("输入错误，请重新输入小数。");
			}
		}
		return value;
	}
}
class Menu {
	public int selectMenu(){
		int result;

		while (true){
			try {
				for(String choice:choices) System.out.println(choice);
				result= KeyboardInput.scanner.nextInt();
				if(result>=0&&result<choices.length) {
					return result;
				}
				System.out.println("Please Enter a number between 0-"
						+(choices.length-1)
				);
			}
			catch (InputMismatchException inputMismatchException){
				System.out.println();
			}
		}

	}
	private String[] choices;
	public Menu(String[] options) {
		this.choices = options;
	}

}
public class Main {
	public static void main(String[] args) {
		KeyboardInput ioListener=new KeyboardInput();
		String[] options;
		while (true)
		{
			System.out.println("请选择输入类型并输入相应内容");
			switch (new Menu(
					options=new String[]{"0.退出", "1.整数", "2.小数", "3.字符串"}
			).selectMenu()) {
				case 0:break;
				case 1:System.out.println("输入的整数是: " + ioListener.readInt(""));continue;
				case 2:System.out.println("输入的小数是: " + ioListener.readDouble(""));continue;
				case 3:System.out.println("输入的字符串是: " + ioListener.readString(""));continue;
			}
			Menu menu = new Menu(options=new String[]{"选项1", "选项2", "选项3"});
			System.out.println("您选择了：" + options[menu.selectMenu()- 1]);
		}
	}
}
